
<div class="panel-default">
  <%= simple_form_for '', url: import_file_admin_hydropower_path, defaults: { required: false }, html: {style: 'display: none', multipart: true, id: "excel_in"} do |f| %>
    <div class="m-l-5">
      <%= file_field_tag :excel, style: 'display:none' %>
    </div>

  <% end %>
  <button type="button" class="layui-btn b-t-15 layui-btn-normal import_file_excel"><i class="layui-icon">&#xe608;</i>导入Excel</button>

  <%= link_to '下载模版', down_file_admin_hydropower_path, method: :post, data:{confirm: '确认下载模版吗？'}, class: 'layui-btn layui-btn' %>
  <table lay-filter="demo" lay-size='lg'>
    <thead>
      <tr>
        <th lay-data="{field:'attr'}" style="width:30%;">属性</th>
        <th lay-data="{field:'attr_value'}">值</th>
      </tr>
    </thead>

    <tbody>

    <tr>
      <td><%= Hydropower.human_attribute_name(:water_balance) %>:</td>
      <td><%= @hydropower.water_balance %></td>
    </tr>

    <tr>
      <td><%= Hydropower.human_attribute_name(:electric_balance) %>:</td>
      <td><%= @hydropower.electric_balance %></td>
    </tr>

    </tbody>
  </table>

  <!-- <div class="actions_show">
    <%= link_to t('buttons.edit'), edit_admin_hydropower_path(@hydropower), class: 'layui-btn' %>
    <%= link_to t('buttons.back'), admin_hydropowers_path, class: 'layui-btn layui-btn-normal' %>
  </div> -->

  <div class="panel-default">
    <%= link_to '导出数据', down_file_admin_hydropower_path(tag: 'build_data'), method: :post, data:{confirm: '确认导出为excel吗？'}, class: 'layui-btn layui-btn' %>
    <table lay-filter="demo" lay-size='lg'>
      <thead>
        <tr>

          <th lay-data="{field:'1'}">所属寝室</th>

          <th lay-data="{field:'2'}">所属寝室总缴电费</th>

          <th lay-data="{field:'3'}">员工名称</th>

          <th lay-data="{field:'31'}">住宿天数</th>

          <th lay-data="{field:'4'}">应缴水费</th>

          <th lay-data="{field:'5'}">应缴电费</th>

          <th lay-data="{field:'6'}">已缴纳</th>

          <th lay-data="{field:'7'}">实际应缴水费</th>

          <th lay-data="{field:'8'}">实际应缴电费</th>

          <th lay-data="{field:'9'}">实际缴费合计</th>
        </tr>
      </thead>

      <tbody>
        <% @hydropower.dorm_users.each do |dorm_user| %>
          <% pay_dorm_user_count = dorm_user.dorm.dorm_users.where('pay_balance > ?', 0).count %>
          <% pay_dorm_total = dorm_user.dorm.dorm_users.sum(:pay_balance) %>
          <!-- 水 -->
          <% day_count_water = (@hydropower.water_balance / @hydropower.dorm_users.sum(:day_count)) * dorm_user.day_count  %>
          <!-- 电 -->
          <% water_pay = (@hydropower.water_balance / dorm_user.dorm.dorm_users.sum(:day_count)) * dorm_user.dorm.dorm_users.where('pay_balance > ?', 0).sum(:day_count) %>
          <% electric_balance = (dorm_user.dorm.electric_balance - (dorm_user.dorm.dorm_users.sum(:pay_balance) - water_pay))%>
          <% day_count_electric = (dorm_user.dorm.electric_balance / dorm_user.dorm.dorm_users.sum(:day_count)) * dorm_user.day_count  %>
          <% day_count_electric2 = (electric_balance / dorm_user.dorm.dorm_users.where.not('pay_balance > ?', 0).sum(:day_count)) * dorm_user.day_count %>
          <tr>

            <td><%= dorm_user.dorm&.name %></td>

            <td><%= dorm_user.dorm&.electric_balance.round(2) %></td>

            <td><%= dorm_user.name %></td>

            <td><%= dorm_user.day_count %></td>

            <td><%= day_count_water.round(2) %></td>

            <td><%= day_count_electric.round(2) %></td>

            <td><%= dorm_user.pay_balance %></td>
            <% if dorm_user.pay_balance > 0 %>
              <%#= water = [(day_count_water - dorm_user.pay_balance), 0].max %>
              <td><%= 0 %></td>
              <td><%= 0 %></td>
              <td><%= 0 %></td>
            <% else %>
              <td><%= day_count_water.round(2) %></td>
              <td><%= day_count_electric2.round(2) %></td>
              <td><%= (day_count_water + day_count_electric2).round(2) %></td>
            <% end %>

          </tr>
        <% end %>
      </tbody>
    </table>
  </div>

</div>

<script>

$(document).on('click', '.import_file_excel', function () {
    $('#excel').click();
})

// 监听导入excel后, 导入数据
$(document).on('change', '#excel', function () {
    if ($(this).val().length <= 0) {
        return
    }
    var file_path = $(this).val().split('\\')
    var title = file_path[file_path.length - 1]
    layer.open({
        type: 0 //此处以iframe举例
        , title: '导入数据'
        , area: ['420px', '230px']
        , shade: 0.3
        , maxmin: true
        , offset: 'auto'
        , content: `您会将文件: '${title}' 导入到系统中, 是否继续?`
        , btn: ['确认', '关闭']
        , yes: function () {
            $('#excel_in').submit();
        }
        , btn2: function () {
            layer.closeAll();
        }
    });
})
</script>